﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Scheduler
{
    /**Class: ManagerPreferences
     * @author Sara Tuohy
     * @version 1.0
     * Course : ITEC 4860 Spring 2012
     * Written: March, 2012
     * This class allows manager to save preferences
     * on a per employee basis
     * Updated to implement the hours and comments save and retrieve.
     */
    public partial class ManagerPreferences : Form
    {
        /*
         * Comment field limit changed to 1000 due to field and table limits            */
        private const int CHARACTER_LIMIT = 1000;
        /*
         * List of all employees names                                                  */
        private Business_Logic_Layer.Employee[] EmployeeList;
        /*
         * List of all employees                                                        */
        private string[] EmployeeSelectionList;
        /**
         * Selected index maps employee name to list                                    */
        private int selectedIndex;
        /**
         * empID is table key                                                           */
        private string empID;
        /**
         * to read and update the Employee table                                        */
        private Data_Access_Layer.Employee employeeData;
        /**
         * store minimum weekly hours                                                   */
        private int minHours;
        /**
         * store maximum weekly hours                                                   */
        private int maxHours;
        /**
        * store manager comments                                                        */
        private string mgrComments;

        /**Method: ManagerPreferences
         * Constructor
         */
        public ManagerPreferences()
        {
            InitializeComponent();
            setup();
        }

        /**Method: setup
         * Prepares the GUI for presentation
         * Get the list of employees for the selection dropdown box
         */
        private void setup()
        {
            if (!Presentation_Layer.GlobalVariables.GlobalEmployeeListCompiled)
            {
                Business_Logic_Layer.GlobalDataManager globalDataMgr = new Business_Logic_Layer.GlobalDataManager();
                globalDataMgr.CreateEmployeeList();
            }
            //Cast the returned list to an array of Employee
            EmployeeList = (Business_Logic_Layer.Employee[])
                Presentation_Layer.GlobalVariables.GlobalEmployeeList;
            EmployeeSelectionList = new string[EmployeeList.Length];
            int i = 0;
            foreach (Business_Logic_Layer.Employee emp in EmployeeList)
            {
                EmployeeSelectionList[i] = emp.Name;
                i++;
            }
            mgrPrefEmpSelComboBox.Items.AddRange(EmployeeSelectionList);

        }

        /**Method: mgrPrefCommentTextbox_TextChanged
         * Tracks sum of characters entered and shows the amount of
         * space remaining in the label adjacent
         */
        private void mgrPrefCommentTextbox_TextChanged(object sender, EventArgs e)
        {
            string totalComment = mgrPrefCommentTextbox.Text;
            int charCount = totalComment.Length;
            int roomLeft = CHARACTER_LIMIT - charCount;
            mpRemCharsLabel.Text = roomLeft.ToString();
        }


        /**Method: mpSaveButton_Click
         * Save the information input
         * Purpose: save the input to the DB
         */
        private void mpSaveButton_Click(object sender, EventArgs e)
        {
            bool goodToGo = true;
            try
            {
                minHours = Convert.ToInt16(mpMinHrsMaskedTextBox1.Text);
                maxHours = Convert.ToInt16(mpMaxHrsMaskedTextBox.Text);
            }
            catch
            {
                MessageBox.Show("Cannot save without entries for Maximum and Minimum Hours", "No Max/Min Selected Error",
                   MessageBoxButtons.OK, MessageBoxIcon.Error);
                goodToGo = false;

            }
            mgrComments = mgrPrefCommentTextbox.Text;
            if (mgrPrefEmpSelComboBox.SelectedIndex < 0)
            {
                MessageBox.Show("Must select employee before attempting to save data.", "No Employee Selected Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                goodToGo = false;
            }
            else if (minHours > maxHours)
            {
                MessageBox.Show("Cannot save while minimum hours is greater than maximum hours.", "Max/Min Hours Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                goodToGo = false;
            }
            else if (goodToGo)
            {
                employeeData = new Data_Access_Layer.Employee(empID.Trim());
                if (employeeData.locateEmployee())
                {
                    employeeData.setMgrMinWeeklyHrs(minHours);
                    employeeData.setMgrMaxWeeklyHrs(maxHours);
                    employeeData.setMgrComment(mgrComments);
                    MessageBox.Show("Data Save Completed");
                    mpSaveButton.Enabled = false;
                    mgrPrefRetrieveButton.Enabled = false;
                }
                else
                {
                    MessageBox.Show("Unable to locate Employee", "Employee Data Save Failure",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /**Method: mpHomeButton_Click
         * Return to calling panel
         * Purpose: close the panel
         */
        private void mpHomeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /**Method: EmployeeSelectionComboBox_SelectedIndexChanged
         * Executes when a new employee is selected.
         * No action should be taken until Retrieve button is clicked
         */
        private void EmployeeSelectionComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            mgrPrefRetrieveButton.Enabled = true;
            //moved this to the retrieveButton_click ... can't save till data retrieved!
            //mpSaveButton.Enabled = true;
        }

        /**Method: mgrPrefRetrieveButton_Click
         * Retreived employee data from DB
         * TODO: implement this method
         */
        private void mgrPrefRetrieveButton_Click(object sender, EventArgs e)
        {
            if (mgrPrefEmpSelComboBox.SelectedIndex < 0)
            {
                MessageBox.Show("Please select employee before attempting to retrieve data.",
                    "ComboBox Retrieve Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                mpSaveButton.Enabled = true;
                selectedIndex = mgrPrefEmpSelComboBox.SelectedIndex;
                empID = EmployeeList[selectedIndex].EmpID;
                employeeData = new Data_Access_Layer.Employee(empID.Trim());
                if (employeeData.locateEmployee())
                {
                    minHours = employeeData.getMgrMinWeeklyHrs();
                    maxHours = employeeData.getMgrMaxWeeklyHrs();
                    mgrComments = employeeData.getMgrComment();
                    mpMinHrsMaskedTextBox1.Text = minHours.ToString();
                    mpMaxHrsMaskedTextBox.Text = maxHours.ToString();
                    mgrPrefCommentTextbox.Text = mgrComments;
                    mpSaveButton.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Unable to locate Employee", "Employee Data Retrieve Failure",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
